<!--用户管理页-->
<template>
  <div style="height:6vh;padding:10px 20px;background-color:#fff;">
    <span style="line-height:60px;font-size:20px;">用户管理</span>
    <el-button type="primary" style="float: right;margin-top:10px;" @click="addUserDialogVisible = true">新建用户</el-button>
  </div>

  <!-- 用户搜索卡片 -->
  <el-card style="margin: 20px;height: 120px;overflow: hidden">
    <el-form :inline="true">
      <el-form-item label="用户搜索" style="width: 270px;">
        <el-input placeholder="输入用户名"
                  v-model="searchUserForm.username"
                  @keydown.enter="loadUser"></el-input>
      </el-form-item>
      <el-form-item label="用户状态" style="width: 270px;">
        <el-select placeholder="请选择用户状态" v-model="searchUserForm.status" @change="loadUser">
          <el-option label="启用" value="1"></el-option>
          <el-option label="禁用" value="0"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="resetSearch">重置</el-button>
        <el-button type="primary" @click="loadUser">查询</el-button>
      </el-form-item>
    </el-form>
    <div >
        <el-button type="success" @click="output">导出</el-button>
        <el-upload v-model:action="importURL" :on-success="importExcel" :on-error="hadleError" :show-file-list="false" style="display: inline-block;margin: 10px">
          <el-button type="success" >导入</el-button>
        </el-upload>
    </div>
  </el-card>
  <!-- 用户数据表格卡片 -->
  <el-card style="margin: 20px;">
    <el-table :data="userArr">
      <el-table-column type="index" label="编号" width="80" align="center"></el-table-column>
      <el-table-column label="用户名" prop="username" align="center"></el-table-column>
      <el-table-column label="手机号" prop="phone" align="center"></el-table-column>
      <el-table-column label="加入时间" prop="createTime" align="center"></el-table-column>
      <el-table-column label="用户状态" prop="status" align="center">
        <template #default="scope">
          <el-switch active-value="1" inactive-value="0" v-model="scope.row.status"></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button link type="primary">密码重置</el-button>
          <el-button link type="primary">编辑</el-button>
          <el-button link type="primary" @click="del(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
  <el-dialog  style="width: 1000px;padding:40px;" title="新建用户" v-model="addUserDialogVisible">
    <el-form label-position="top">
      <el-row :gutter="10">
        <el-col :span="12">
          <el-form-item label="用户名">
            <el-input placeholder="请输入用户名" v-model="userAddForm.username"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="手机号">
            <el-input placeholder="请输入手机号" v-model="userAddForm.phone"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="12">
          <el-form-item label="邮箱" >
            <el-input placeholder="请输入邮箱" v-model="userAddForm.email"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="年龄">
            <el-input placeholder="请输入年龄" v-model="userAddForm.age"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="6">
          <el-form-item label="职级">
            <el-select placeholder="请选择职级" v-model="userAddForm.level">
              <el-option label="普通用户" value="1"></el-option>
              <el-option label="管理员" value="2"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="直属领导" >
            <el-select placeholder="请选择" v-model="userAddForm.parentId">
              <el-option label = "经理" value="10"></el-option>
              <el-option label = "员工"value="20"></el-option>
              <el-option label = "部门经理" value="30"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="性别" >
            <el-radio-group v-model="userAddForm.gender">
              <el-radio border label="男" value="1" style="margin: 0;">男</el-radio>
              <el-radio border label="女" value="2"></el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-form-item label="用户状态" >
          <el-radio-group v-model="userAddForm.status">
            <el-radio border label="启用" style="margin: 0;" value="1"></el-radio>
            <el-radio border label="禁用" value="0"></el-radio>
          </el-radio-group>
        </el-form-item>
      </el-row>
    </el-form>
    <div style="float: right">
      <el-button type="text" >取消</el-button>
      <el-button type ="primary" @click="saveUser">保存</el-button>
    </div>
  </el-dialog>
</template>

<script setup>
import {onMounted, ref} from "vue";
import axios from "axios";
import qs from "qs";
import {ElMessage} from "element-plus";

//定义对象来保存用户查询的条件
const searchUserForm = ref({username:'',status:''});
//定义对象保存查询到的用户表格数据
const userArr = ref([]);
//定义加载用户列表的方法
const loadUser = ()=>{
  //如果有查询条件,就根据条件查,没有条件默认查全部数据
  //axios.get(BASE_URL+'/v1/user/select?username='+ searchUserForm.value.username+ '&status='+searchUserForm.value.status)
  let data = qs.stringify(searchUserForm.value)
  //将请求的JS对象用qs转为查询字符串格式再发送给后端 ? username=xxx&status=xxx
  axios.get(BASE_URL+'/v1/user/select?'+data)
      .then((response)=>{//回调函数,处理响应数据
        if(response.data.code==2000){//后端返回的状态码是2000说明成功了
          userArr.value = response.data.data;//把请求回来的数据装到绑定表格的数组中
          console.log(userArr.value);
        }else{
          ElMessage.error(response.data.message);
        }
      })
}
//页面一加载完毕,立即执行
onMounted(()=>{
  loadUser();
})

//重置搜索
const resetSearch = ()=>{
  //清空之前的搜索内容
  searchUserForm.value = {username:'',status:''};
  //重新加载所有的用户数据
  loadUser();
}
/*=================添加用户====================*/
//定义添加用户的属性
const userAddForm = ref({
  username:'',
  phone:'',
  email:'',
  age:'',
  level:'',
  parentId:'',
  gender:'',
  status:''
    }
);
const addUserDialogVisible = ref(false);
//定义添加用户的方法
const saveUser =()=>{
  console.log(userAddForm.value);
  let data = qs.stringify(userAddForm.value)
  axios.post(BASE_URL+'/v1/user/save',data).then((response)=>{
    console.log(response.data)
    if(response.data.code==2000){
      ElMessage.success('添加用户');
      //添加成功后,关闭弹窗
      addUserDialogVisible.value = false;
      //刷新用户列表
      loadUser();
    }else{
      ElMessage.error(response.data.data);
    }
  })
}
//================定义删除的方法========================
const del =(userId)=>{
  axios.post(BASE_URL+'/v1/user/delete/'+userId).then((response)=>{
    if(response.data.code==2000){
      ElMessage.success('删除成功');
      loadUser();
    }else{
      ElMessage.error(response.data.message);
    }
  })
}
//==============导出用户列表=====================
const output = ()=>{
  // 构建查询字符串
  const params = new URLSearchParams(userAddForm.value).toString();
  // 直接打开下载链接，浏览器会自动处理下载
  // window.open(`${BASE_URL}/v1/user/output?${params}`, '_blank');
  window.open(BASE_URL+'/v1/user/output?'+params,'_blank');
  ElMessage.success('正在导出，请查看下载文件');  //导出成功后提示`
}
//==============导入用户上传的excel表格==============
//拼接url
const importURL = ref(BASE_URL+'/v1/user/import');
const importExcel =(res,file,fileList)=>{
  console.log('上传响应:', res);
  console.log('上传文件:', file);
  console.log('上传文件个数',fileList)

  if (res.code === 2000) {
    ElMessage.success(res.message || '导入成功');
    loadUser(); // 刷新数据
  } else {
    ElMessage.error(res.message || '导入失败');
  }
};
//上传错误处理
const handleError = (error, file, fileList) => {
  console.error('上传错误:', error);
  ElMessage.error('文件上传失败');
};
</script>





<style scoped>

</style>